package cn.chen.miaosha.controller;

import cn.chen.miaosha.domain.Goods;
import cn.chen.miaosha.domain.MiaoshaUser;
import cn.chen.miaosha.domain.OrderInfo;
import cn.chen.miaosha.result.CodeMsg;
import cn.chen.miaosha.result.Result;
import cn.chen.miaosha.service.GoodsService;
import cn.chen.miaosha.service.OrderService;
import cn.chen.miaosha.vo.GoodsVO;
import cn.chen.miaosha.vo.OrderDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    GoodsService goodsService;

    @RequestMapping("/detail")
    @ResponseBody
//    @NeedLogin
    public Result<OrderDetailVO> info(Model model, MiaoshaUser user, @RequestParam("orderId") Long orderId){
        if(user == null){
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        OrderInfo order = orderService.getOrderById(orderId);
        if(order == null){
            return Result.error(CodeMsg.ORDER_NOT_EXIST);
        }
        Long goodsId = order.getGoodsId();
        GoodsVO goods = goodsService.getGoodsVOById(goodsId);
        OrderDetailVO vo = new OrderDetailVO();
        vo.setGoods(goods);
        vo.setOrder(order);
        return Result.success(vo);
    }

}
